package com.act.moudles.user.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.act.components.UserComponent;
import com.act.constant.LoginTypeEnums;
import com.act.constant.MenuConstant;
import com.act.moudles.user.entity.Menu;
import com.act.moudles.user.entity.User;
import com.act.moudles.user.service.ComboService;
import com.act.moudles.user.service.MenuService;
import com.act.moudles.user.service.SecretKeyService;
import com.act.moudles.user.service.UserService;
import com.act.utils.DataResult;
import com.act.utils.vueMenu.VueUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "通用请求")
@Slf4j
public class IndexController {

    @Resource
    private UserComponent userComponent;

    @Resource
    private UserService userService;

    @Resource
    private ComboService comboService;

    @Resource
    private MenuService menuService;

    @Resource
    private SecretKeyService secretKeyService;

    @PostMapping("login")
    @ApiOperation("账号登录")
    public DataResult login(@RequestBody User loginUser){
        log.info("登录参数：{} / {}",loginUser.getLoginName(),loginUser.getPassword());
        //1.空验
        if(StringUtils.isEmpty(loginUser.getLoginName()) &&
                StringUtils.isEmpty(loginUser.getPhonenumber())){
            return DataResult.fail("用户名或者手机号不能为空！");
        }
        //2.用户名或者手机号查询
        User user = userService.getByUseNameOrPhone(loginUser.getLoginName(),loginUser.getLoginName());
        if(user == null){
            return DataResult.fail("用户名或者手机号不存在！");
        }
        //3.登录
        StpUtil.login(user.getUserId(),LoginTypeEnums.WEB.getType());
        //4.缓存基础用户信息
        userComponent.setUserBasicSession(user, LoginTypeEnums.WEB.getType());
        return DataResult.success("登录成功",StpUtil.getTokenValue());
    }

    @GetMapping("/getInfo")
    @ApiOperation("获取到当前用户的个人信息")
    public DataResult getInfo(){
        Map<String,Object> map = new HashMap<>();
        User user = userComponent.getCurrentUser(LoginTypeEnums.WEB.getType());
        map.put("permissions", userComponent.getPermissionList(user.getUserId(),null));
        map.put("roles", userComponent.getRoleList(user.getUserId(),null));
        map.put("user", user);
        return DataResult.success("查询成功!",map);
    }


    @GetMapping("logout")
    @ApiOperation("推出登录")
    public DataResult loginOut(){
        StpUtil.logout(StpUtil.getLoginId(),LoginTypeEnums.WEB.getType());
        return DataResult.success("退出成功");
    }

    @GetMapping("getRouters")
    @ApiOperation("构建Vue动态路由")
    public DataResult vueRouter(){
        //1.查询角色菜单

        List<Menu> menuList = comboService.listComboDetail(userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()).getComboId());
        //2. 国旅数据
        menuList = menuList.stream().filter(o-> o.getMenuType().equals(MenuConstant.TYPE_DIR)
                || o.getMenuType().equals(MenuConstant.TYPE_MENU)).collect(Collectors.toList());

        List<Menu>  comMenuList = menuService.buildMenuChildren(menuList);
        //3.构建路由
        return DataResult.success("查询成功！",VueUtil.buildMenus(comMenuList));
    }


    @PostMapping("findSecretKey")
    @ApiOperation("查询登录密钥")
    public DataResult  findSecretKey(@RequestBody String json){
        JSONObject obj = JSON.parseObject(json);
        String userName = obj.getString("userName");
        String pwd = obj.getString("pwd");
        String secret = obj.getString("secret");
        if(StringUtils.isEmpty(userName)){
            return DataResult.fail("缺少用户名：userName");
        }
        if(StringUtils.isEmpty(pwd)){
            return DataResult.fail("缺少密码：pwd");
        }
        if(StringUtils.isEmpty(secret)){
            return DataResult.fail("缺少密钥：secret");
        }
        try {
            String token = secretKeyService.findSaTokenBySecretValue(secret,userName,pwd);
            return DataResult.success("查询成功！",token);
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

    @GetMapping("testSecret")
    @ApiOperation("测试登录密钥")
    public DataResult  testSecret(){
        return DataResult.success("查询成功啦");
    }

}
